// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/sale/widgets/QGAlert.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class HFSelectWalletPage extends StatefulWidget {
  const HFSelectWalletPage({super.key});
  @override
  State<HFSelectWalletPage> createState() => _HFSelectWalletPageState();
}

class _HFSelectWalletPageState extends State<HFSelectWalletPage> {
  Map<String, dynamic>? userInfo;
  bool isPayFee = false;

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    onRefresh();
  }

  onRefresh() {
    if (User.isLogin) {
      Network.get(url_hf_checkHuifuFee).then((res) async {
        isPayFee = res.success == true;
        if (isPayFee) {
          setState(() {});
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
      bgColor: Color(0xFF0C1012),
      title: '选择钱包',
      body: Column(
        children: [
          Container(height: 0.5.rpx, color: Color(0xFFEAEAEA)),
          SizedBox(height: 15.rpx),
          buildInfo(),
          SizedBox(height: 10.rpx),
          // buildAnother(),
        ],
      ),
    );
  }

  buildInfo() {
    return GestureDetector(
      onTap: () {
        isPayFee ? toContinueWallet() : toOpenWallet();
      },
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 10.rpx),
        child: Stack(
          children: [
            QGImage(asset: "mine_wallet_bg", height: 140.rpx),
            Column(
              children: [
                SizedBox(height: 17.5.rpx),
                buildWallet(),
                SizedBox(height: 45.rpx),
                isPayFee ? buildContinueButton() : buildOpenButton(),
              ],
            )
          ],
        ),
      ),
    );
  }

  buildAnother() {
    return Container(
      width: kScreenWidth,
      height: 60.rpx,
      alignment: Alignment.center,
      margin: EdgeInsets.symmetric(horizontal: 10.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.rpx),
        color: Color(0xFFF8F8F6),
      ),
      child: Text(
        "更多通道，敬请期待",
        style: TextStyle(
            fontSize: 12.rpx,
            fontWeight: FontWeight.normal,
            color: Color.fromRGBO(17, 17, 17, 0.8)),
      ),
    );
  }

  buildWallet() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(width: 22.5.rpx),
        QGImage(asset: "mine_huifu_icon", width: 40.rpx),
        SizedBox(width: 15.rpx),
        Text(
          "汇付支付",
          style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16.rpx,
              color: Colors.white),
        )
      ],
    );
  }

  buildOpenButton() {
    return Container(
      padding: EdgeInsets.only(right: 20.5.rpx),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            "立即开通",
            style: TextStyle(
                fontWeight: FontWeight.normal,
                fontSize: 12.rpx,
                color: Colors.white),
          ),
          // Transform.rotate(
          //   angle: pi,
          //   child: QGImage(asset: "nav_back_white", width: 12.rpx),
          // )
        ],
      ),
    );
  }

  buildContinueButton() {
    return Container(
      padding: EdgeInsets.only(right: 20.5.rpx),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            "继续开通",
            style: TextStyle(
                fontWeight: FontWeight.normal,
                fontSize: 12.rpx,
                color: Colors.white),
          ),
          // Transform.rotate(
          //   angle: pi,
          //   child: QGImage(asset: "nav_back_white", width: 12.rpx),
          // )
        ],
      ),
    );
  }

  toOpenWallet() {
    if (!User.isLogin) {
      Navigator.of(context).pushNamed(PageLogin);
      return;
    }
    if (userInfo?['realname_check'] == '0') {
      toast("实名认证后才能开通钱包");
      return;
    }

    QGAlert.show(context,
        title: "开通汇付支付",
        content: "开通汇付支付需要2元开通费",
        confirm: "确认",
        onConfirm: toConfirm);
  }

  Future<bool> checkPermission() async {
    await Permission.camera.request();
    await Permission.microphone.request();
    await Permission.photos.request();
    return true;
  }

  toContinueWallet() {
    Network.post(url_hf_openWallet, context: context).then((res) async {
      if (!res.success) {
        toast(res.msg);
        return;
      }

      checkPermission();
      Navigator.of(context).pushReplacementNamed(PageQGPayWebView,
          arguments: res.data["redirect_url"] ?? "");
    });
    // Navigator.of(context).pushNamed(PageOpenWallet);
  }

  toConfirm() {
    Network.post(url_hf_walletFeeOrder, context: context).then((res) async {
      if (!res.success) {
        toast(res.msg);
        return;
      }

      await Future.delayed(const Duration(seconds: 1));
      Navigator.of(context).pop();

      var url = res.data["pay_url"];
      if (url is String) {
        Navigator.of(context).pushNamed(PageQGPayWebView, arguments: url);
      }
    });
  }
}
